//
//  UIViewController+ChoicePhotoHepler.m
//  mcrmSAAS
//
//  Created by yuxiuyi on 16/11/16.
//  Copyright © 2016年 GuangZhouXuanWu. All rights reserved.
//

#import "UIViewController+ChoicePhotoHepler.h"

@implementation UIViewController (ChoicePhotoHepler)

-(void)choicePhotoWithMaxCount:(NSUInteger)maxCount sourceType:(UIImagePickerControllerSourceType)sourceType
{
    [UIViewController closeKeyBoard];
    
    if ([XMSFileHelper getSystemFreeSize] / 1024 / 1024 < 150)
    {
        [UIAlertView showInfoMsg:@"存储空间不足，未能获取图片"];
        return;
    }
    
#if !TARGET_IPHONE_SIMULATOR
    if ([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
    {
        if(sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
        {
            if (![SystemRightJudge checkPhotoAuthorization])
                return;

            DoImagePickerController *controller = [DoImagePickerController new];
            controller.nMaxCount = maxCount;
            controller.nColumnCount = 4;
            controller.delegate = self;
            [UIViewController presentViewController:controller animated:YES completion:NULL];
        }
        else if(sourceType == UIImagePickerControllerSourceTypeCamera)
        {
            if (![SystemRightJudge checkCameraAuthorization])
                return;
            
            UIImagePickerController *controller = [UIImagePickerController new];
            controller.delegate = self;
            controller.sourceType = UIImagePickerControllerSourceTypeCamera;
            [UIViewController presentViewController:controller animated:YES completion:NULL];
        }
    }
    else
    {
        
    }
#else
    if(sourceType == UIImagePickerControllerSourceTypePhotoLibrary)
    {
        DoImagePickerController *controller = [DoImagePickerController new];
        controller.nMaxCount = maxCount;
        controller.nColumnCount = 4;
        controller.delegate = self;
        [UIViewController presentViewController:controller animated:YES completion:NULL];
    }
    else if(sourceType == UIImagePickerControllerSourceTypeCamera)
    {
        [self imageOperation:@[[UIImage imageWithMainBundle:@"Icon-120.png"]]];
    }
#endif
}

-(void)imageOperation:(NSArray*)images
{

}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
    [self imageOperation:@[image]];
    
    [UIViewController dismissViewControllerAnimated:YES completion:NULL];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [UIViewController dismissViewControllerAnimated:YES completion:NULL];
}

-(void)didSelectPhotosFromDoImagePickerController:(DoImagePickerController *)picker result:(NSArray *)aSelected
{
    [self imageOperation:aSelected];
    
    [UIViewController dismissViewControllerAnimated:YES completion:NULL];
}

-(void)didCancelDoImagePickerController
{
    [UIViewController dismissViewControllerAnimated:YES completion:NULL];
}

@end
